package Singleton;

import java.io.Serializable;

public class Singleton3 implements Serializable {
     private Singleton3(){
        if (INSTANCE!=null){  // if语句放止反射破坏单例模式
            throw new RuntimeException("单例对象不能重复创建");
        }
        System.out.println("所有单例模式中构造器都需要私有化3");
    }

    private static Singleton3 INSTANCE = null;

     // synchronized 保证线程安全
    public static synchronized Singleton3 getInstance() {
        if (INSTANCE ==null){
            INSTANCE = new Singleton3();  // 多线程下多个线程都会进入该语句
        }
        return INSTANCE;
    }

    public static void otherMethod(){
        System.out.println("Singleton3：otherMethod");
    }

    // 放止反序列化破坏单例模式
    public Object readResolve(){
        return INSTANCE;
    }
}
